package com.bjpowernode.shop.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

//认证服务器
@Configuration
public class WebServerConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    //配置认证方式
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置认证方式=数据库认证，配置认证方法=userDetailsService
        //基于数据库的认证操作，userDetailsService是登录时调用的真正的认证方法
        auth.userDetailsService(userDetailsService);
    }

    @Bean
    @Override
    //认证管理器。在授权时用于
    //如果没有认证管理器，则无法识别密码授权方式
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    //密码加密器。在授权时用于第三方的账号密码的加密操作
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
